#include "downloaderwindow.h"

DownloaderWindow::DownloaderWindow(QWidget *parent) :
    QMainWindow(parent)
{
    this->resize(900, 300);
    myTable = new QTableWidget(0,5);
    myTable->setItemDelegateForColumn(4, new StarDelegate);
    myTable->setItemDelegateForColumn(3, new ProgressBarDelegate(this));
    myTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    myTable->setSelectionBehavior(QAbstractItemView::SelectRows);
    QStringList headerLabels;
    headerLabels << "Number" << "LocalAddress" << "InternetAddress" << "Progress" << "Priority";
    myTable->setHorizontalHeaderLabels(headerLabels);

    this->setCentralWidget(myTable);
    QMenu *fileMenu = menuBar()->addMenu("File");
    QMenu *helpMenu = menuBar()->addMenu("Help");
    QAction *action = fileMenu->addAction("New download");
    QAction *helpAction = helpMenu->addAction("Show help");
    connect(action, SIGNAL(triggered()), this, SLOT(startNewDownload()));
    connect(helpAction, SIGNAL(triggered()), this, SLOT(showHelp()));
    myTable->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
    Config::Instance();

    downloadService = new DownloadService();
    connect(downloadService, SIGNAL(progressChanged(const DownloadInfo*, double)), this,
            SLOT(progressChanged(const DownloadInfo*, double)));
    show();
    QMessageBox *box = new QMessageBox(QMessageBox::Information, "Attention!", "All uncompleted downloads will be restarted.");
    box->exec();
    for (QList<DownloadInfo>::const_iterator i = Config::Instance().getDownloadsList().begin(); i != Config::Instance().getDownloadsList().end(); ++i){
        if (i->getProgress() < 1) {
            downloadService->addDownload(&(*i));
        }
        addInfoToTable(*i);
    }
    myTable->setSortingEnabled(true);
    show();
}

void DownloaderWindow::onClose() {
    Config::Instance().saveToDB();
}

void DownloaderWindow::startNewDownload(){
    NewDownloadDialog *downloadDialog = new NewDownloadDialog();
    if(downloadDialog->exec()){
        DownloadInfo newDownload(downloadDialog->getLocalAddress(), downloadDialog->getInternetAddress(), downloadDialog->getPriority(), 0);
        Config::Instance().getDownloadsList().append(newDownload);
        downloadService->addDownload(&Config::Instance().getDownloadsList().back());
        addInfoToTable(newDownload);
    }
    show();
}


void DownloaderWindow::progressChanged(const DownloadInfo* download, double progress){
    const_cast<DownloadInfo*>(download)->setProgress(progress);
    myTable->viewport()->update();
}

void DownloaderWindow::addInfoToTable(DownloadInfo info) {
    QTableWidgetItem *item0 = new QTableWidgetItem;
    QTableWidgetItem *item1 = new QTableWidgetItem;
    QTableWidgetItem *item2 = new QTableWidgetItem;
    QTableWidgetItem *item3 = new QTableWidgetItem;
    MyTableWidgetItem *item4 = new MyTableWidgetItem;
    item0->setData(0, info.getNumber());
    item1->setData(0, info.getLocalAddress());
    item2->setData(0, info.getInternetAddress());
    item3->setData(0, info.getProgress());
    item4->setData(0,
                   qVariantFromValue(StarRating(info.getPriority())));
    static int lastRowNumber = -1;
    myTable->setRowCount(lastRowNumber + 2);
    ++lastRowNumber;
    myTable->setItem(lastRowNumber, 0, item0);
    myTable->setItem(lastRowNumber, 1, item1);
    myTable->setItem(lastRowNumber, 2, item2);
    myTable->setItem(lastRowNumber, 3, item3);
    myTable->setItem(lastRowNumber, 4, item4);
}

void DownloaderWindow::showHelp(){
    QMessageBox *box = new QMessageBox(QMessageBox::Information, "Help!", "To start new downlad select File->New download.");
    box->exec();
}

const DownloadInfo* const DownloaderWindow::getByRow(int row) const{
    quint32 number = myTable->item(row, 0)->text().toInt();
    for (QList<DownloadInfo>::const_iterator i = Config::Instance().getDownloadsList().begin(); i != Config::Instance().getDownloadsList().end(); ++i) {
        if (i->getNumber() == number) {
            return &(*i);
        }
    }
    return 0;
}
